package abc.com.abc.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.security.Guard;

import abc.com.abc.R;
import abc.com.abc.utils.MD5Utils;

public class LoginActivity extends AppCompatActivity {
    private TextView tv_main_title;
    private TextView tv_back,tv_register,tv_find_psw;
    private Button btn_login,btn_login_gly;
    private String userName1,psw,spPsw,GLYusername,GLYpassword;
    private EditText et_user_name,et_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏
        init();
        
    }

    private void init() {
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText("登陆");
        tv_back=findViewById(R.id.tv_back);
        tv_register = findViewById(R.id.tv_register);
        tv_find_psw = findViewById(R.id.tv_find_psw);
        tv_register = findViewById(R.id.tv_register);
        btn_login = findViewById(R.id.btn_login);
        et_user_name = findViewById(R.id.et_username);
        et_psw = findViewById(R.id.et_password);
        btn_login_gly= findViewById(R.id.btn_login_GLY);
        //返回按钮
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            LoginActivity.this.finish();
            }
        });
        //立即注册
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        //找回密码
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到找回密码界面，代写界面中
                Intent intent = new Intent(LoginActivity.this,FindPswActivity.class);
                startActivity(intent);
            }
        });
        //登陆学员按钮
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName1 = et_user_name.getText().toString().trim();
                psw = et_psw.getText().toString().trim();
                String md5Pswv= MD5Utils.md5(psw);
                spPsw = readPsw(userName1);

                Log.i("readPsw",spPsw);
                if (TextUtils.isEmpty(userName1)){
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(psw)){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if(md5Pswv.equals(spPsw)){
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    saveLoginStatus(true,userName1);
                    Intent data = new Intent();
                    data.putExtra("isLogin",true);
                    setResult(RESULT_OK,data);
                    LoginActivity.this.finish();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);

                    return;
                }else if (!TextUtils.isEmpty(spPsw)&&!md5Pswv.equals(spPsw)){
                    Toast.makeText(LoginActivity.this,"用户名密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    Toast.makeText(LoginActivity.this,"此用户名不存在",Toast.LENGTH_SHORT).show();
                    return;
                }

            }

        });
        //


        //登陆管理员按钮
        final SharedPreferences spp = getSharedPreferences("name",MODE_PRIVATE);
        btn_login_gly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GLYusername = et_user_name.getText().toString();
                GLYpassword = et_psw.getText().toString();
                if (TextUtils.isEmpty(GLYusername)){
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(GLYpassword)){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if (GLYusername.equals("name")&&GLYpassword.equals("pass")){

                    SharedPreferences.Editor editor = spp.edit();
                    editor.putString("username",GLYusername);
                    editor.putString("password",GLYpassword);
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this,GlySuccessActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(LoginActivity.this,"用户名密码错误!请重新输入",Toast.LENGTH_SHORT).show();

                }
         }
        });
    }

    // 保存登陆状态和登陆用户名到SharedPreferences
    private void saveLoginStatus(boolean b, String userNmae) {
        //loginInfo表示文件名
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin",b);
        editor.putString("loginUserName",userNmae);
        editor.commit();

    }
    //根据用户名读取密码

    private String readPsw(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        return sp.getString(userName,"");
    }
    //获取回传数据
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data!=null){
            String userName = data.getStringExtra("userName");
            if (!TextUtils.isEmpty(userName)){
                et_user_name.setText(userName);
                //设置光标的位置
                et_user_name.setSelection(userName.length());
            }
        }

    }
}


